package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Attendance;
import cn.wolfcode.crm.domain.ClaimsClientDetails;
import cn.wolfcode.crm.domain.InsureInfo;
import cn.wolfcode.crm.mapper.ClaimsClientDetailsMapper;
import cn.wolfcode.crm.query.ClaimsClientDetailsQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IClaimsClientDetailsService;
import cn.wolfcode.crm.service.IInsureInfoService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class ClaimsClientDetailsServiceImpl implements IClaimsClientDetailsService {


    @Autowired
    private IInsureInfoService insureInfoService;

    @Autowired
    private ClaimsClientDetailsMapper claimsClientDetailsMapper;

    public int deleteByPrimaryKey(Long id) {
        return claimsClientDetailsMapper.deleteByPrimaryKey(id);
    }

    public int insert(Long clientId, Long carId, Long insureinfoId) {
        return claimsClientDetailsMapper.insert(clientId, carId, insureinfoId);
    }

    public ClaimsClientDetails selectByPrimaryKey(Long id) {
        return claimsClientDetailsMapper.selectByPrimaryKey(id);
    }

    public List<ClaimsClientDetails> selectAll() {
        return claimsClientDetailsMapper.selectAll();
    }

    public int updateByPrimaryKey(ClaimsClientDetails record) {
        return claimsClientDetailsMapper.updateByPrimaryKey(record);
    }

    public PageResult query(ClaimsClientDetailsQueryObject qo) {
        //保存操作
        List<InsureInfo> insureInfos = insureInfoService.listAllForClaimsClientDetails();
        if (insureInfos.size() > 0) {
            for (int i = 0; i < insureInfos.size(); i++) {
                System.out.println("已经理赔的客户: " + insureInfos.get(i));
                ClaimsClientDetails claimsClientDetails = new ClaimsClientDetails();

                ClaimsClientDetails claimsClientDetails1 = claimsClientDetailsMapper.selectByClientId(insureInfos.get(i).getClient().getId());
                if (claimsClientDetails1 != null) {
                    continue;
                }
                claimsClientDetails.setClient(insureInfos.get(i).getClient());
                claimsClientDetails.setCar(insureInfos.get(i).getCar());
                claimsClientDetails.setInsureInfo(insureInfos.get(i));
                claimsClientDetailsMapper.insert(insureInfos.get(i).getClient().getId(), insureInfos.get(i).getCar().getId(), insureInfos.get(i).getId());
            }
        }
        //==========
        int count = claimsClientDetailsMapper.queryCount(qo);
        if (count == 0) {
            return new PageResult(0, Collections.emptyList());
        }
        List<ClaimsClientDetails> rows = claimsClientDetailsMapper.queryList(qo);
        return new PageResult(count, rows);
    }

    public ClaimsClientDetails selectByClientId(Long id) {
        return claimsClientDetailsMapper.selectByClientId(id);
    }


}
